/*
author:杨文超
email:yangwenchao@keynection.cn
*/
#pragma once
#include "esp_err.h"
#include "esp_netif.h"
#include "esp_event.h"

#ifdef __cplusplus
extern "C"
{
#endif

ESP_EVENT_DECLARE_BASE(MODEM_BOARD_EVENT);

typedef enum {
    MODEM_EVENT_DTE_CONN = 0,        /*!< Modem DTE Connected */
    MODEM_EVENT_DTE_DISCONN,         /*!< Modem DTE Disconnected */
    MODEM_EVENT_SIMCARD_CONN,        /*!< Modem SIM CARD Connected */
    MODEM_EVENT_SIMCARD_DISCONN,     /*!< Modem SIM CARD Disconnected */
    MODEM_EVENT_DTE_RESTART,         /*!< Modem DTE Reset to restart */
    MODEM_EVENT_DTE_RESTART_DONE,    /*!< Modem DTE Restart done */
    MODEM_EVENT_NET_CONN,            /*!< Modem Net Connected */
    MODEM_EVENT_NET_DISCONN,         /*!< Modem Net Disconnected */
    MODEM_EVENT_WIFI_STA_CONN,       /*!< Modem Wi-Fi new station Connected */
    MODEM_EVENT_WIFI_STA_DISCONN,    /*!< Modem Wi-Fi station disconnected */
    MODEM_EVENT_UNKNOWN              /*!< Modem Unknown Response */
} modem_event_t;

#define MODEM_DEFAULT_CONFIG()\
    {                                \
        .rx_buffer_size = 1024*15,   \
        .tx_buffer_size = 1024*15,   \
        .line_buffer_size = 1600,    \
        .event_task_priority = CONFIG_USBH_TASK_BASE_PRIORITY + 1,\
        .event_task_stack_size = 3072\
    }

#define MODEM_FLAGS_INIT_NOT_FORCE_RESET   (1UL<< 1)   /* If set, will not reset 4g modem using reset pin during init */
#define MODEM_FLAGS_INIT_NOT_ENTER_PPP     (1UL<< 2)   /* If set, will not enter ppp mode during init */
#define MODEM_FLAGS_INIT_NOT_BLOCK         (1UL<< 3)   /* If set, will not wait until ppp got ip before modem_board_init return */

typedef struct {
    int rx_buffer_size;             /* USB RX Buffer Size */
    int tx_buffer_size;             /* USB TX Buffer Size */
    int line_buffer_size;           /* Line buffer size for command mode */
    int event_task_priority;        /* USB Event/Data Handler Task Priority*/
    int event_task_stack_size;      /* USB Event/Data Handler Task Stack Size*/
    esp_event_handler_t handler;    /* Modem event handler */
    void *handler_arg;              /* Modem event handler arg */
    int flags;                      /* Modem config flag bits */
} modem_config_t;

/**
modem初始化
param config modem board config value
 */
esp_err_t modem_board_init(modem_config_t *config);

/**
 函数：通过ppp接口获取dns信息
 参数：
 * type 要获取的dns的类型: ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP
 * dns  用于返回成功获取到dns信息
 */
esp_err_t modem_board_get_dns_info(esp_netif_dns_type_t type, esp_netif_dns_info_t *dns);

/**
 获取modem的信号质量
 */
esp_err_t modem_board_get_signal_quality(int *rssi, int *ber);

/**
判断sim卡是否被正确探测到是否被锁定
 */
esp_err_t modem_board_get_sim_cart_state(int *if_ready);

/**
通过reset管脚强制重置modem
 */
esp_err_t modem_board_force_reset(void);

/**
 * @brief This function is used to get the operator's name, otherwise return ESP_FAIL if the network is not registered.
 *
 * @param buf buffer to store name
 * @param buf_size buffer size
 * @return ** esp_err_t
 */
esp_err_t modem_board_get_operator_state(char *buf, size_t buf_size);

/**
设置ppp拨号接入网络的方式
 */
esp_err_t modem_board_set_apn(const char *new_apn, bool force_enable);

/**
ppp自动重新拨号
 */
esp_err_t modem_board_ppp_auto_connect(bool enable);

/**
 开始ppp拨号
 */
esp_err_t modem_board_ppp_start(uint32_t timeout_ms);

/**
 停止ppp拨号
 */
esp_err_t modem_board_ppp_stop(uint32_t timeout_ms);

#ifdef __cplusplus
}
#endif
